
const app = getApp();
const api = app.apiInstance
import { getUserInfo } from '../../utils/store';
Page({
  data: {
    currentStep: 0,
    tabStatus: [{
      label: '未填写',
      value: ''
    }, {
      label: '进行中',
      value: 'process'
    }, {
      label: '已完成',
      value: 'finish'

    }],
    windowWidth: 0,
    shenBaoId: "",
    subStatusList: [
      {
        value: 'Complete',
        label: '完成'
      },
      {
        value: 'NotYetStarted',
        label: '未开始'
      },
      {
        value: 'Pass',
        label: '通过'
      },
      {
        value: 'Reject',
        label: '拒绝'
      },
    ],

    statusList: [
      {
        value: 'AcceptanceCompleted',
        label: '完成验收'
      },
      {
        value: 'Cancellation',
        label: '作废'
      },
      {
        value: 'Reject',
        label: '拒绝'
      },
      {
        value: 'ToBeAcceptance',
        label: '待验收'
      },
      {
        value: 'ToBeConfirmed',
        label: '待确认'
      },
      {
        value: 'ToBeReviewed',
        label: '待审核'
      }
    ],

    staffList: [],
    isEdit:true,
    formData:{},
    id:''

  },
  async loadData() {
    const staffList = await api.get(`/v1/openApi/employee/search/staffList`);
    this.setData({
     staffList:staffList.rows
   })
   if(this.data.id){
    const formData = await api.get(`/v1/openApi/resource/change/details/${this.data.id}`);
     this.setData({
      formData:formData.data.key.contentMap
   })
   }
  },


  onLoad(params) {

    this.setData({
      isEdit:params.isEdit=='false'?false:true,
      id:params.id,
      shenBaoId:params.id
    })


    this.loadData();
    var systemInfo = wx.getSystemInfoSync();
    this.setData({
      windowWidth: systemInfo.windowWidth
    })

  },

  swiperChange(e) {
    this.setData({
      currentStep: e.detail.current

    })
  },

  goPre() {
    this.setData({
      currentStep: this.data.currentStep - 1
    })
  },
  goNext() {
    this.setData({
      currentStep: this.data.currentStep + 1
    })
  },

  async submit() {

    if(!this.data.isEdit){
      wx.navigateBack({
        delta: 1
      });
      return;
    }
    const jiBenShenBao = this.selectComponent("#jiBenShenBao").data
    const xiaoFangShenBao = this.selectComponent("#xiaoFangShenBao").data
    const zhiYeJkShenBao = this.selectComponent("#zhiYeJkShenBao").data
    const huanBaoLeiShenBao = this.selectComponent("#huanBaoLeiShenBao").data
    const resDeptYj = await api.get('/deptAudit/wx/list', { dept: "应急管理部" })
    const resDeptZjl = await api.get('/deptAudit/wx/list', { dept: "总经理办公室" })
    console.log('jiBenShenBao',jiBenShenBao.form)
    console.log('xiaoFangShenBao',xiaoFangShenBao.form)
    console.log('zhiYeJkShenBao',zhiYeJkShenBao.form)
    console.log('huanBaoLeiShenBao',huanBaoLeiShenBao.form)
    if (!jiBenShenBao.form.projectName) {
      wx.showToast({
        title: '项目名称不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!jiBenShenBao.form.leadDept || jiBenShenBao.form.leadDept.length == 0) {
      wx.showToast({
        title: '项目主导部门不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }

    if (!jiBenShenBao.form.projectTracer) {
      wx.showToast({
        title: '项目主导部门不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }



    
      let step = 1;
      const processStatuses = [];
      //确认部门确认人
      processStatuses.push({
        auditor: jiBenShenBao.form.projectTracerSapId,
        status: 'NotYetStarted',
        step: ++step,
        remark: '确认'
      })
      //消防审核
      const xiaoFang = resDeptYj.rows.find(item => item.auditKey == '变更管理——审核——消防')
      ++step
      xiaoFang.auditPersons.length > 0 && xiaoFang.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '消防审核'
        })
      })


      //职业健康安全审核
      const zhiYeJk = resDeptYj.rows.find(item => item.auditKey == '变更管理——审核——职业健康安全部')
      ++step
      zhiYeJk.auditPersons.length > 0 && zhiYeJk.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '职业健康审核'
        })
      })


      //环保审核
      const huanBao = resDeptYj.rows.find(item => item.auditKey == '变更管理——审核——环保')
      ++step
      huanBao.auditPersons.length > 0 && huanBao.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '环保审核'
        })
      })


      //安全部审核
      const anQuan = resDeptYj.rows.find(item => item.auditKey == '变更管理——审核——安全部')
      ++step
      anQuan.auditPersons.length > 0 && anQuan.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '安全审核'
        })
      })


      //应急管理部审核
      const yingJiGl = resDeptYj.rows.find(item => item.auditKey == '变更管理——审核——应急管理部')
      ++step
      yingJiGl.auditPersons.length > 0 && yingJiGl.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '应急管理审核'
        })
      })




      //分管副总审核
      const fenGuanFz = resDeptZjl.rows.find(item => item.auditKey == '变更管理——审核——分管副总')
      ++step
      fenGuanFz.auditPersons.length > 0 && fenGuanFz.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '分管副总审核'
        })
      })


      //副总审核
        const fuZong = resDeptZjl.rows.find(item => item.auditKey == '变更管理——审核——副总')
        ++step
        fuZong.auditPersons.length > 0 && fuZong.auditPersons.map(item => {
          processStatuses.push({
            auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
            status: 'NotYetStarted',
            step: step,
            remark: '副总审核'
          })
        })
      //总经理审核
      const ZongJinLi = resDeptZjl.rows.find(item => item.auditKey == '变更管理——审核——总经理')
      ++step
      ZongJinLi.auditPersons.length > 0 && ZongJinLi.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '总经理审核'
        })
      })
      //项目跟进人验收
      processStatuses.push({
        auditor: jiBenShenBao.form.projectTracerSapId,
        status: 'NotYetStarted',
        step: ++step,
        remark: '项目跟进人验收'
      })
      
      const deptRes = await api.get('/deptInfo/wx/'+jiBenShenBao.form.leadDept)
      const manageSapid = this.data.staffList.find(item => item.staffName == deptRes.data.deptManager)?.sapId

      console.log('manageSapid',manageSapid)

      //主导部门经理验收
      processStatuses.push({
        auditor: manageSapid,
        status: 'NotYetStarted',
        step: ++step,
        remark: '使用部门经理验收'
      })
      console.log('111',jiBenShenBao?.form)
      //相关部门验收
      for (let item of jiBenShenBao?.form?.checkDepart||[]) {
        let deptResXg = await api.get('/deptInfo/wx/'+item)
        processStatuses.push({
          auditor: this.data.staffList.find(item => item.staffName == deptResXg.data.deptManager)?.sapId,
          status: 'NotYetStarted',
          step: ++step,
          remark: `相关部门验收——${this.data.staffList.find(item => item.staffName == deptResXg.data.deptManager)?.dept}`
        })
      }
      console.log('1')
      //消防验收
      ++step
      xiaoFang.auditPersons.length > 0 && xiaoFang.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '消防验收'
        })
      })
      console.log('2')
      //职业健康安全验收
      ++step
      zhiYeJk.auditPersons.length > 0 && zhiYeJk.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '职业健康验收'
        })
      })
      console.log('3')
      //环保验收
      ++step
      huanBao.auditPersons.length > 0 && huanBao.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '环保验收'
        })
      })
      console.log('4')
      //安全部验收
      ++step
      anQuan.auditPersons.length > 0 && anQuan.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '安全验收'
        })
      })
      console.log('5')
      //应急管理部验收
      ++step
      yingJiGl.auditPersons.length > 0 && yingJiGl.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '应急管理验收'
        })
      })
      console.log('6')
      //分管副总验收
      ++step
      fenGuanFz.auditPersons.length > 0 && fenGuanFz.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '分管副总验收'
        })
      })
      //副总验收
      ++step
      fuZong.auditPersons.length > 0 && fuZong.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '副总验收'
        })
      })
      //总经理验收
      ++step
      ZongJinLi.auditPersons.length > 0 && ZongJinLi.auditPersons.map(item => {
        processStatuses.push({
          auditor: this.data.staffList.find(subItem => subItem.id == item)?.sapId,
          status: 'NotYetStarted',
          step: step,
          remark: '总经理验收'
        })
      })
      console.log('环保',this.selectComponent("#huanBaoLeiShenBao").getForm());
      const params = {
        id: this.data.shenBaoId,
        sapid: getUserInfo().siocSapNo,
        name: jiBenShenBao.form.projectName,
        contactMan: jiBenShenBao.form.projectTracer,
        status: "ToBeConfirmed",
        content: JSON.stringify({shenBao:{ ...jiBenShenBao.form, 
          ...xiaoFangShenBao.form, 
          ...this.selectComponent("#zhiYeJkShenBao").getForm(),
          ...this.selectComponent("#huanBaoLeiShenBao").getForm(),
        
        }}),
        processStatuses
      }

      // const jiBenShenBao = this.selectComponent("#jiBenShenBao").data
      // const xiaoFangShenBao = this.selectComponent("#xiaoFangShenBao").data
      // const zhiYeJkShenBao = this.selectComponent("#zhiYeJkShenBao").data
      // const huanBaoLeiShenBao = this.selectComponent("#huanBaoLeiShenBao").data

      //修改
    if (this.data.shenBaoId) {
      api.post(`/v1/openApi/resource/change/updateResourceChangeManagement/${getUserInfo().siocSapNo}`, params).then(res => {
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'none',
          duration: 2000
        })
      })
    }
    //添加
    else {
      api.post(`/v1/openApi/resource/change/saveResourceChangeManagement/${getUserInfo().siocSapNo}`, params).then(res => {
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'none',
          duration: 2000
        })
      })
    }


      

    
  }

})
